package com.study.clothes.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.study.clothes.pojo.Car;
import com.study.clothes.pojo.RespBean;
import com.study.clothes.service.ICarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Jiayu.Yang
 * @since 2021-12-29
 */
@RestController
@RequestMapping("/car")
public class CarController {
    @Autowired
    private ICarService icarService;
    @GetMapping("/")
    public List<Car> CarList(){
        return icarService.list();

    }
    @GetMapping("/get")
    public Car getCar(Integer goodId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("goodsId",goodId);
        return icarService.getOne(queryWrapper);
    }
    @GetMapping("/getSum")
    public Integer getSum(Integer id){
        return icarService.getSum(id);

    }


    @PostMapping("/")
    public RespBean add(Car car) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userId", car.getUserId());
        queryWrapper.eq("goodsId", car.getGoodsId());
        Car one = icarService.getOne(queryWrapper);
        if (one!=null){
            Car car1 = one.setNum(one.getNum() + 1);
            icarService.updateById(car1);
            return RespBean.success("添加成功");
        }
        if (icarService.save(car)) {
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");

    }
    @PutMapping("/")
    public RespBean update(Car car){
        if (icarService.updateById(car)) {
            return RespBean.success("修改成功");
        }
        return RespBean.error("修改失败");
    }
    @PutMapping("/update")
    public RespBean updateByGoodsId(Car car){
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("goodsId", car.getGoodsId());
        if (icarService.update(updateWrapper)) {
            return RespBean.success("修改成功");
        }
        return RespBean.error("修改失败");
    }
    @DeleteMapping("/{id}")
    public RespBean deleteOrder(@PathVariable Integer id){
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("goodsid", id);
        if (icarService.remove(updateWrapper)) {
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");

    }
}
